package dao

import (
	"github.com/crack007/go-im/src/common/entity"
	"github.com/crack007/go-im/src/common/util"
	"gorm.io/gorm"
)

type userDao struct {
	db *gorm.DB
}

func (d *userDao) GetByAccount(account string) *entity.UserEntity {
	var e *entity.UserEntity
	d.db.Where("is_delete=0 and account=?", account).Find(&e)
	if e.ID == 0 {
		return nil
	}
	return e
}

func (d *userDao) GetByUserId(userId uint32) *entity.UserEntity {
	var e *entity.UserEntity
	d.db.Where("is_delete=0 and user_id=?", userId).Find(&e)
	if e.ID == 0 || e.IsDelete {
		return nil
	}
	return e
}

func (d *userDao) Save(userEntity *entity.UserEntity) {
	if userEntity.ID > 0 {
		userEntity.UpdatedAt = util.GetDatetime()
	}
	d.db.Save(userEntity)
}
